module page {

    export class WorldRankPage extends egret.DisplayObjectContainer {

        public static readonly Page_Close: string = "World_Rank_Page_Close";

        public postData: any;

        private gameData: any;

        public constructor() {
            super();

            this.bigRank = new rank.BigRank();
            this.addChild(this.bigRank);
        }

        private onClose(): void {
            this.dispatchEvent(new egret.Event(WorldRankPage.Page_Close));
        }

        public reload():void {
            this.loadRankData();
        }

        private loadRankData(): void {
            let postData: any = {};
            postData.token = manager.GameManager.getInstance().getStatisticManager().appToken;
            postData.page = 0;
            postData.count = 100;
            let request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            let url: string = manager.StatisticsManager.domain
                + '/index.php?g=WxaGame&m=GameRank&a=get_rank&page=0&count=100&session_3rd='
                + manager.GameManager.getInstance().session_3rd + "&token=" + postData.token;
            this.log(url, postData);
            request.open(url, egret.HttpMethod.GET);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send(postData);
            request.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onLoadError, this);
        }

        private log(url: string, data): void {
            egret.log("请求地址:", url);
            egret.log("请求体:", data);
        }

        private onLoadComplete(event: egret.Event): void {
            egret.log("获取世界排行完成");
            let request = <egret.HttpRequest>event.currentTarget;
            let resp = JSON.parse(request.response);
            console.log(resp);
            this.showRank(resp);
        }

        private onLoadError(event: egret.Event): void {
            egret.log("获取世界排行失败");
            let request = <egret.HttpRequest>event.currentTarget;
            let resp = JSON.parse(request.response);
            console.log(resp);
        }

        public bigRank: rank.BigRank;

        private userScore: number;
        private userRank: number;

        private showRank(res): void {
            if (!res.data) return;
            this.gameData = res.data;
            this.userScore = res.user_score;
            this.userRank = res.user_rank;

            let infos: Array<rank.ScoreItem> = [];
            this.gameData = this.gameData.sort(function (a: any, b: any) {
                return b.score - a.score;
            });
            this.gameData.forEach(function (info, idx) {
                if (info.username != null) {
                    let item = new rank.ScoreItem(info.wecha_id, info.portrait, info.score, idx + 1, info.username);
                    infos.push(item);
                }
            });
            this.bigRank.update(infos);
        }

        public hide(): void {
            this.visible = false;
            manager.DisplayManager.remove(this);
        }

        public show():void {
            this.visible = true;
        }
    }

}